package sardine;

import sardine.function.Route;

/**
 * @auth bruce-sha
 * @date 2015/5/21
 */
public abstract class SimpleResponseTransformerRoute<T> extends SimpleRoute<T> {

    protected SimpleResponseTransformerRoute(String path, String acceptType) {
        super(path, acceptType);
    }

    public abstract String render(T model) throws Exception;


    public static <T> SimpleResponseTransformerRoute<T> create(String path,
                                                        Route<T> route,
                                                        ResponseTransformer<T> transformer) {
        return create(path, SardineBase.DEFAULT_ACCEPT_TYPE, route, transformer);
    }

    public static <T> SimpleResponseTransformerRoute<T> create(String path,
                                                        String acceptType,
                                                        Route<T> route,
                                                        ResponseTransformer<T> transformer) {
        return new SimpleResponseTransformerRoute<T>(path, acceptType) {

            @Override
            public T apply(Request request, Response response) throws Exception {
                return route.apply(request, response);
            }

            @Override
            public String render(T model) throws Exception {
                return transformer.render(model);
            }
        };
    }
}
